<div class="{{$viewClass['form-group']}} {!! !$errors->has($errorKey) ? '' : 'has-error' !!}">

    <label for="{{$id}}" class="{{$viewClass['label']}} control-label">{{$label}}</label>

    <div class="{{$viewClass['field']}}">

        @include('extensions.field.error')

        <div class="jstree-wrapper {{$class}}-tree-wrapper">
            <div>
                <label class="checkbox-inline" style="padding-left: 0;">
                    <input class="tree-select-all" type="checkbox">&nbsp;&nbsp;全选
                </label>
                <label class="checkbox-inline" style="padding-left: 0;">
                    <input class="tree-expand" type="checkbox" checked>&nbsp;&nbsp;展开
                </label>
            </div>
            <div class="da-tree" style="margin-top:10px"></div>
        </div>
        <input {!! $attributes !!} name="{{$name}}"/>

        @include('extensions.field.help-block')

    </div>
</div>

<script data-exec-on-popstate>
    /**
     * 获取json对象或数组的长度
     * @param obj
     * @returns {number}
     *
     */
    function len(obj){
        if (typeof obj !== 'object') {
            return 0;
        }
        let i, len = 0;
        for(i in obj) {
            len += 1;
        }
        return len;
    }

     /**
     * 判断元素是否在对象中存在
     * @param arr
     * @param val
     * @param strict
     * @returns {boolean}
     */
    function inObject(arr, val, strict) {
        if (len(arr) < 1) {
            return false;
        }
        for (var i in arr) {
            if (strict) {
                if (val === arr[i]) {
                    return true;
                }
                continue
            }
            if (val == arr[i]) {
                return true;
            }
        }
        return false;
    }

    $(function () {
        var $tree = $('.{{$class}}-tree-wrapper').find('.da-tree'),
            opts = {!! $options !!},
            $input = $('input[name="{{$name}}"]'),
            parents = {!! $parents !!};
        opts.core = opts.core || {};
        opts.core.data = {!! $nodes !!};
        $('.tree-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.tree-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $tree.jstree("check_all");
            } else {
                $tree.jstree("uncheck_all");
            }
        });
        $('.tree-expand').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.tree-expand').on('ifChanged', function(event) {
            if (this.checked) {
                $tree.jstree("open_all");
            } else {
                $tree.jstree("close_all");
            }
        });
        $tree.on("changed.jstree", function (e, data) {
            $input.val('');
            var i, selected = [];
            for (i in data.selected) {
                if (inObject(parents, data.selected[i])) { // 过滤父节点
                    continue;
                }
                selected.push(data.selected[i]);
            }
            selected.length && $input.val(selected.join(','));
        }).on("loaded.jstree", function () {
            $tree.jstree('open_all');
        }).jstree(opts);
    });
</script>
